package com.lysj.bhp.exchangerate.controller;

import com.lysj.bhp.exchangerate.base.ExchangeRateContent;
import com.lysj.bhp.exchangerate.domain.ExchangeRate;
import com.lysj.bhp.exchangerate.vo.ExchangeRateVo;
import com.lysj.bhp.util.random.FormatUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/exchange_rate")
public class ExchangeRateController extends ExchangeRateContent {


    @RequestMapping("list")
    public Resp list() {
        List<ExchangeRateVo> exchangeRateVos = new ArrayList<>();

        ExchangeRateVo etherumToDollar = new ExchangeRateVo();
        etherumToDollar.setExchangeRateVoId(1L);
        etherumToDollar.setName("以太币对应美元");
        etherumToDollar.setRate(exchangeRateService.getEthereumToDollar());
        etherumToDollar.setDateStatus(ExchangeRate.DATA_RESOURCE);
        etherumToDollar.setExpires(ExchangeRate.ETHEREUM_TO_DOLLAR_EXPIRES);
        etherumToDollar.setIsRate(1);
        exchangeRateVos.add(etherumToDollar);

        ExchangeRateVo bitcoinToDollar = new ExchangeRateVo();
        bitcoinToDollar.setExchangeRateVoId(2L);
        bitcoinToDollar.setName("比特币对应美元");
        bitcoinToDollar.setRate(exchangeRateService.getBitcoinToDollar());
        bitcoinToDollar.setDateStatus(ExchangeRate.DATA_RESOURCE);
        bitcoinToDollar.setExpires(ExchangeRate.BITCOIN_TO_DOLLAR_EXPIRES);
        bitcoinToDollar.setIsRate(1);
        exchangeRateVos.add(bitcoinToDollar);

        ExchangeRateVo cnyToDollar = new ExchangeRateVo();
        cnyToDollar.setExchangeRateVoId(3L);
        cnyToDollar.setName("人民币对应美元");
        cnyToDollar.setRate(exchangeRateService.cnyToDollarRate());
        cnyToDollar.setDateStatus(ExchangeRate.DATA_RESOURCE);
        cnyToDollar.setExpires(ExchangeRate.CNY_TO_DOLLAR_EXPIRES);
        cnyToDollar.setIsRate(1);
        exchangeRateVos.add(cnyToDollar);

        ExchangeRateVo bhpcToDollar = new ExchangeRateVo();
        bhpcToDollar.setExchangeRateVoId(4L);
        bhpcToDollar.setName("算力币对应美元");
        bhpcToDollar.setRate(exchangeRateService.getBhpcToDollar());
        bhpcToDollar.setDateStatus(ExchangeRate.DATA_RESOURCE);
        bhpcToDollar.setExpires(ExchangeRate.BHPC_TO_DOLLAR_EXPIRES);
        bhpcToDollar.setIsRate(1);
        exchangeRateVos.add(bhpcToDollar);


        ExchangeRateVo diamoundToDollar = new ExchangeRateVo();
        diamoundToDollar.setExchangeRateVoId(5L);
        diamoundToDollar.setName("比特币砖石对应美元");
        diamoundToDollar.setRate(exchangeRateService.getDiamoundToDollar());
        diamoundToDollar.setDateStatus(ExchangeRate.DATA_RESOURCE);
        diamoundToDollar.setExpires(ExchangeRate.DIAMOUND_TO_DOLLAR_EXPIRES);
        diamoundToDollar.setIsRate(1);
        exchangeRateVos.add(diamoundToDollar);

        ExchangeRateVo buToDollar = new ExchangeRateVo();
        buToDollar.setExchangeRateVoId(6L);
        buToDollar.setName("布比币对应美元");
        buToDollar.setRate(exchangeRateService.getBuToDollar());
        buToDollar.setDateStatus(ExchangeRate.DATA_RESOURCE);
        buToDollar.setExpires(ExchangeRate.BU_TO_DOLLAR_EXPIRES);
        buToDollar.setIsRate(1);
        exchangeRateVos.add(buToDollar);

        ExchangeRateVo bhpcToCny = new ExchangeRateVo();
        bhpcToCny.setExchangeRateVoId(7L);
        bhpcToCny.setName("算力币对应人民币");
        bhpcToCny.setRate(exchangeRateService.bhpcToCnyRate());
        bhpcToCny.setDateStatus(ExchangeRate.DATA_RESOURCE);
        bhpcToCny.setExpires(System.currentTimeMillis());
        bhpcToCny.setIsRate(0);
        exchangeRateVos.add(bhpcToCny);

        ExchangeRateVo bitcoinToCny = new ExchangeRateVo();
        bitcoinToCny.setExchangeRateVoId(8L);
        bitcoinToCny.setName("比特币对应人民币");
        bitcoinToCny.setRate(exchangeRateService.bitcoinToCnyRate());
        bitcoinToCny.setDateStatus(ExchangeRate.DATA_RESOURCE);
        bitcoinToCny.setExpires(System.currentTimeMillis());
        bitcoinToCny.setIsRate(0);
        exchangeRateVos.add(bitcoinToCny);


        ExchangeRateVo ethToCny = new ExchangeRateVo();
        ethToCny.setExchangeRateVoId(9L);
        ethToCny.setName("以太坊对应人民币");
        ethToCny.setRate(exchangeRateService.ethToCnyRate());
        ethToCny.setDateStatus(ExchangeRate.DATA_RESOURCE);
        ethToCny.setExpires(System.currentTimeMillis());
        ethToCny.setIsRate(0);
        exchangeRateVos.add(ethToCny);

        ExchangeRateVo dollarToCny = new ExchangeRateVo();
        dollarToCny.setExchangeRateVoId(10L);
        dollarToCny.setName("美元对应人民币");
        dollarToCny.setRate(exchangeRateService.dollarToCnyRate());
        dollarToCny.setDateStatus(ExchangeRate.DATA_RESOURCE);
        dollarToCny.setExpires(System.currentTimeMillis());
        dollarToCny.setIsRate(0);
        exchangeRateVos.add(dollarToCny);

        ExchangeRateVo diamoundToCny = new ExchangeRateVo();
        diamoundToCny.setExchangeRateVoId(11L);
        diamoundToCny.setName("比特币砖石对应人民币");
        diamoundToCny.setRate(exchangeRateService.diamoundToCnyRate());
        diamoundToCny.setDateStatus(ExchangeRate.DATA_RESOURCE);
        diamoundToCny.setExpires(System.currentTimeMillis());
        diamoundToCny.setIsRate(0);
        exchangeRateVos.add(diamoundToCny);

        ExchangeRateVo buToCny = new ExchangeRateVo();
        buToCny.setExchangeRateVoId(12L);
        buToCny.setName("布比币对应人民币");
        buToCny.setRate(exchangeRateService.buToCnyRate());
        buToCny.setDateStatus(ExchangeRate.DATA_RESOURCE);
        buToCny.setExpires(System.currentTimeMillis());
        buToCny.setIsRate(0);
        exchangeRateVos.add(buToCny);

        return new Resp(exchangeRateVos);
    }

    @PostMapping("edit")
    public Resp edit(BigDecimal rate, String expires, Long exchangeRateVoId) throws Exception {
        if (rate.compareTo(new BigDecimal(BigInteger.ZERO)) != 1) {
            return new Resp(Resp.PARAM_ERROR, "基础汇率大于0");
        }
        if (ParamUtil.isBlack(expires)) {
            return new Resp(Resp.PARAM_ERROR, "请输入缓存有效时间");
        }
        long date = FormatUtil.stringTodates(expires).getTime();
        if (exchangeRateVoId == 1) {
            ExchangeRate.ETHEREUM_TO_DOLLAR = rate;
            ExchangeRate.ETHEREUM_TO_DOLLAR_EXPIRES = date;
        } else if (exchangeRateVoId == 2) {
            ExchangeRate.BITCOIN_TO_DOLLAR = rate;
            ExchangeRate.BITCOIN_TO_DOLLAR_EXPIRES = date;
        } else if (exchangeRateVoId == 3) {
            ExchangeRate.CNY_TO_DOLLAR = rate;
            ExchangeRate.CNY_TO_DOLLAR_EXPIRES = date;
        } else if (exchangeRateVoId == 4) {
            ExchangeRate.BHPC_TO_DOLLAR = rate;
            ExchangeRate.BHPC_TO_DOLLAR_EXPIRES = date;
        } else if (exchangeRateVoId == 5) {
            ExchangeRate.DIAMOUND_TO_DOLLAR = rate;
            ExchangeRate.DIAMOUND_TO_DOLLAR_EXPIRES = date;
        } else if (exchangeRateVoId == 6) {
            ExchangeRate.BU_TO_DOLLAR = rate;
            ExchangeRate.BU_TO_DOLLAR_EXPIRES = date;
        } else {
            return new Resp(Resp.PARAM_ERROR, "非基础汇率不支持修改");
        }
        return new Resp("修改成功");
    }

    /**
     * @param model
     * @return 修改数据源
     */
    @PostMapping("/editData")
    public Resp editData(ExchangeRateVo model) {
        ExchangeRate.DATA_RESOURCE = model.getDateStatus();
        ExchangeRate.ETHEREUM_TO_DOLLAR_EXPIRES = System.currentTimeMillis();
        ExchangeRate.BITCOIN_TO_DOLLAR_EXPIRES = System.currentTimeMillis();
        ExchangeRate.CNY_TO_DOLLAR_EXPIRES = System.currentTimeMillis();
        ExchangeRate.BHPC_TO_DOLLAR_EXPIRES = System.currentTimeMillis();
        ExchangeRate.DIAMOUND_TO_DOLLAR_EXPIRES = System.currentTimeMillis();
        ExchangeRate.BU_TO_DOLLAR_EXPIRES = System.currentTimeMillis();
        return new Resp("修改成功");
    }

    @GetMapping("rates")
    public Resp rates() {
        Map<String, String> map = new HashMap<>();
        map.put("btcToDollar", exchangeRateService.getBitcoinToDollar().toString());
        map.put("bhpcToDallar", exchangeRateService.getBhpcToDollar().toString());
        map.put("ethToDollar", exchangeRateService.getEthereumToDollar().toString());
        map.put("bhpcToBcny", exchangeRateService.bhpcToCnyRate().toString());
        return new Resp(map);
    }

}
